/*eslint-disable*/

const Controller = require('egg').Controller;

// const Mock=require('../mock')

// const fs=require('fs')

const jwt = require('jsonwebtoken')

const json = require('./mock.json')

class HomeController extends Controller {
  async mock() {
    const { ctx, app } = this;
    // const data=fs.writeFileSync(`${__dirname}/mock.json`,JSON.stringify(Mock),()=>{ })
    const data = await app.mysql.insert('watch', json)
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }

  //房源信息数据
  async list() {
    const { ctx, app } = this;
    const data = await app.mysql.select('watch')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }


  // 房源信息编辑
  async listedit() {
    const {ctx,app}=this

    const {english,title,word,num,id}=ctx.request.body

    const data=await app.mysql.update('watch',{english,title,word,num,id})
    ctx.body={
      code:'200',
      msg:'编辑成功',
      data
    }
  }


  //房源信息增加
  async listadd() {
    const { ctx, app } = this

    const { english, title,num, word } = ctx.request.body

    const data = await app.mysql.insert('watch', { english, title, word,num })

    ctx.body = {
      code: '200',
      msg: '请求成功',
      data
    }
  }
  

  //房源信息数据删除
  async listdel() {
    const { ctx, app } = this;
    const {id} = ctx.query
    await app.mysql.delete('watch', { id: id })
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }


  //房源信息数据搜索
  async listsearch() {

    const { ctx, app } = this
    const { value } = ctx.request.body

    const data = await app.mysql.query(`select * from watch where name like "%${value}%"`)
    ctx.body = {
      code: '200',
      msg: '请求成功',
      data
    }
  }
  //团购管理
  async list3() {
    const { ctx, app } = this;
    const data = await app.mysql.select('tuangou')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }


  //咨询管理删除
  async list3del() {
    const { ctx, app } = this;
    const { id } = ctx.query
    console.log(ctx.query);
    await app.mysql.delete('tuangou', { id: id })
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }


  //审核管理
  async list4() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shenhe')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }


   //审核管理
   async list4del() {
    const { ctx, app } = this;
    const { id } = ctx.query
    console.log(ctx.query);
    await app.mysql.delete('shenhe', { id: id })
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }


   //审核管理
   async list4edit() {
    const { name, title,id } = ctx.request.body
    const data = await app.mysql.update('shenhe', {name, title,id })
    ctx.body = {
      code: '200',
      msg: '请求成功',
      data
    }
  }


  //咨询管理
  async list6() {
    const { ctx, app } = this;
    const data = await app.mysql.select('zixun')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }
  //咨询管理删除
  async list6del() {
    const { ctx, app } = this;
    const { id } = ctx.query
    console.log(ctx.query);
    await app.mysql.delete('zixun', { id: id })
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }
  // 房源信息编辑
  async list6edit() {
    const { ctx, app } = this

    const { name, title, id } = ctx.request.body

    const date = await app.mysql.update('zixun', {id, name, title })

    ctx.body = {
      code: '200',
      msg: '请求成功',
      date
    }
  }

  //房源信息增加
  async list6add() {
    const { ctx, app } = this

    const { name, title } = ctx.request.body
    console.log(ctx.request.body);

    const date = await app.mysql.insert('zixun', { name, title })

    ctx.body = {
      code: '200',
      msg: '请求成功',
      date
    }
  }

  //房源信息数据搜索
  async list6search() {
    const { ctx, app } = this;
    const { value } = ctx.query;
    // console.log(ctx.query);
    const data = await this.app.mysql.select('zixun', {
      columns: ['id', 'title'], //查询字段，全部查询则不写，相当于查询*

      where: {
        title: '尚品水居即将开盘'
      }, //查询条

      orders: [
        ['id', 'desc'] 
      ],
    })
    ctx.body = {
      code: 200,
      msg: "分类成功",
      data
    }
  }
  //订单管理数据
  async list5() {
    const { ctx, app } = this;
    const data = await app.mysql.select('dingdan')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }

  //订单管理删除
  async list5del() {
    const { ctx, app } = this;
    const { key } = ctx.query
    console.log(ctx.query);
    await app.mysql.delete('dingdan', { key: key })
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }

  //经纪人管理
  async list7() {
    const { ctx, app } = this;
    const data = await app.mysql.select('jingjiren')
    ctx.body = {
      code: 200,
      msg: "请求成功",
      data
    }
  }
   //经纪人删除
   async list7del() {
    const { ctx, app } = this;
    const { id } = ctx.query
    console.log(ctx.query);
    await app.mysql.delete('jingjiren', { id:id})
    ctx.body = {
      code: 200,
      msg: "删除成功",
    }
  }

  // 房源信息编辑
  async list7edit() {
    const { ctx, app } = this

    const { name, phone,gong,xin,ershou,zu,zhuang, id } = ctx.request.body

    const date = await app.mysql.update('jingjiren', {name, phone,gong,xin,ershou,zu,zhuang, id })

    ctx.body = {
      code: '200',
      msg: '请求成功',
      date
    }
  }

  //房源信息增加
  async list7add() {
    const { ctx, app } = this

    const { name, phone,gong,xin,ershou,zu,zhuang} = ctx.request.body
    console.log(ctx.request.body);

    const date = await app.mysql.insert('jingjiren', { name, phone,gong,xin,ershou,zu,zhuang})

    ctx.body = {
      code: '200',
      msg: '请求成功',
      date
    }
  }

  //房源信息数据搜索
  async list7search() {
    const { ctx, app } = this;
    const { value } = ctx.query;
    // console.log(ctx.query);
    const data = await this.app.mysql.select('', {
      columns: ['id', 'title'], //查询字段，全部查询则不写，相当于查询*

      where: {
        title: '尚品水居即将开盘'
      }, //查询条

      orders: [
        ['id', 'desc'] 
      ],
    })
    ctx.body = {
      code: 200,
      msg: "分类成功",
      data
    }
  }
   // 登录
  async login() {
    const { ctx, app } = this;

    const { username, password } = ctx.request.body

    const data = await app.mysql.get('user', { username, password })

    if (data) {
      const token = jwt.sign(data.username, 'wln')

      ctx.body = {
        code: '1',
        msg: '登录成功',
        token
      }
    } else {
      ctx.body = {
        code: '2',
        msg: '用户名密码错误'
      }
    }
  }
  // 注册
  async reset() {
    const { ctx, app } = this;

    const { username, password } = ctx.request.body

    const data = await app.mysql.get('user', { username })

    if (data) {
      ctx.body = {
        code: '1',
        msg: '账号已存在',
      }
    } else {
      const data = await app.mysql.insert('user', { username, password })

      ctx.body = {
        code: '2',
        msg: '注册成功',
        data
      }
    }
  }
}

module.exports = HomeController;
